﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Newtonsoft.Json;

namespace Json.test
{
    public class Theme
    {
        public Brush Background { get; set; }
    }
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void BrushLoad(object sender,RoutedEventArgs e)
        {
            var fs = System.IO.File.OpenRead(@"G:\1.the");
            var data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);
            fs.Close();
            var theme = JsonConvert.DeserializeObject<Theme>(Encoding.UTF8.GetString(data));
            this.Background = theme.Background;
        }
        private void ImageLoad(object sender,RoutedEventArgs e)
        {
            var fs = System.IO.File.OpenRead(@"G:\1.the");
            var data = new byte[fs.Length];
            fs.Read(data, 0, data.Length);
            fs.Close();
            var theme = JsonConvert.DeserializeObject<Theme>(Encoding.UTF8.GetString(data));
            this.Background = theme.Background;
        }
        private void SaveBrush(object sender,RoutedEventArgs e)
        {
            this.Background = new SolidColorBrush(Color.FromRgb(0, 122, 204));
            var theme = new Theme() { Background = this.Background };
            var data = JsonConvert.SerializeObject(theme);
            var f = System.IO.File.OpenWrite(@"G:\1.the");
            var sw = new System.IO.StreamWriter(f);
            sw.Write(data);
            sw.Close();
        }
        private void SaveImage(object sender, RoutedEventArgs e)
        {
            this.Background = new ImageBrush(new BitmapImage(new Uri(@"G:\1.bmp")));
            if(this.Background is ImageBrush)
            {

            }
            var data = JsonConvert.SerializeObject(((ImageBrush)this.Background).ImageSource);
            var f = System.IO.File.OpenWrite(@"G:\1.the");
            var sw = new System.IO.StreamWriter(f);
            sw.Write(data);
            sw.Close();
        }
    }
}
